package project.suntray.com.core.net;

import java.util.ArrayList;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

import commerce.zhangyang.com.core.app.ConfigureType;
import commerce.zhangyang.com.core.app.Natte;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

/**
 * Created by zhangyang on 2017/12/15 05:24.
 * version 1
 * restCreator 主要是:
 *  Retrofit 和 RetrofitClient 创建!
 */
public class RestCreator{


    public static final class PARAMS_HOLDER{
        //对应的参数信息
        public static final WeakHashMap<String,Object> PARAMS=new WeakHashMap<String,Object>();
    }

    /**
     * 返回对应的参数信息
     * @return
     */
    public static final WeakHashMap getParams(){
        return  PARAMS_HOLDER.PARAMS;
    }
    /**
     * OKHttpClient 类
     */
    public static final class OKHTTPCLIENT_HOLDER{
        public static final int TIME_OUT=60; //时间超时
        public static final OkHttpClient.Builder BUILDER=new OkHttpClient.Builder(); //okHttpClient的构建者
        public static final ArrayList<Interceptor> INTECEPTORS= Natte.getConfiguration(ConfigureType.INTERCEPTORS.name());

        /**
         * 获取 OKHttpClient的Builder的类
         * @return
         */
        public static final OkHttpClient.Builder interceptors(){
            for(Interceptor interceptor:INTECEPTORS){
                BUILDER.addInterceptor(interceptor);
            }
            return BUILDER;
        }


        /**
         * 配置 okHttpClient类
         */
        public static final OkHttpClient OK_HTTP_CLIENT=interceptors().
                                                connectTimeout(TIME_OUT, TimeUnit.SECONDS).
                                                        readTimeout(TIME_OUT,TimeUnit.SECONDS).
                                                            writeTimeout(TIME_OUT,TimeUnit.SECONDS).build();
    }

    /**
     *  retrofit的内部类
     */
    public static final class RetrofitHolder{
        public static final String BASE_URL= Natte.getConfiguration(ConfigureType.API_HOST.name());
        public static final Retrofit RETROFIT= new Retrofit.Builder().baseUrl(BASE_URL).client(OKHTTPCLIENT_HOLDER.OK_HTTP_CLIENT).build();
    }


    /**
     * 获取对应的RestService
     * @return
     */
    public static final RestService getRestService(){
        return RetrofitHolder.RETROFIT.create(RestService.class);
    }
}
